<template>
  <div class="players">
    <div class="page-header">
      <h1>玩家列表</h1>
      <div class="header-actions">
        <div class="search-box">
          <input
            v-model="searchQuery"
            type="text"
            placeholder="搜索玩家..."
            class="search-input"
          />
        </div>
        <div class="sort-controls">
          <select v-model="sortBy" class="sort-select">
            <option value="level">按等级排序</option>
            <option value="experience">按经验排序</option>
            <option value="accuracy">按准确率排序</option>
            <option value="casesSolved">按破案数排序</option>
            <option value="username">按用户名排序</option>
          </select>
        </div>
      </div>
    </div>
    
    <PlayerList
      :players="filteredPlayers"
      title=""
      :show-add-friend="playerStore.isLoggedIn"
      :show-pagination="true"
      :page-size="12"
      @add-friend="handleAddFriend"
      @view-profile="handleViewProfile"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { usePlayerStore } from '@/stores/player';
import { PlayerList } from '@/components';
import type { Player } from '@/entities/Player';

const router = useRouter();
const playerStore = usePlayerStore();

// 状态
const searchQuery = ref('');
const sortBy = ref('level');

// 计算属性
const filteredPlayers = computed(() => {
  let players = playerStore.players;
  
  // 搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase();
    players = players.filter(player => 
      player.username.toLowerCase().includes(query) ||
      player.nickname.toLowerCase().includes(query)
    );
  }
  
  // 排序
  players = [...players].sort((a, b) => {
    switch (sortBy.value) {
      case 'level':
        return b.level - a.level;
      case 'experience':
        return b.stats.experience - a.stats.experience;
      case 'accuracy':
        return b.stats.accuracy - a.stats.accuracy;
      case 'casesSolved':
        return b.stats.casesSolved - a.stats.casesSolved;
      case 'username':
        return a.username.localeCompare(b.username);
      default:
        return 0;
    }
  });
  
  return players;
});

// 方法
const handleAddFriend = async (playerId: string) => {
  try {
    await playerStore.addFriend(playerId);
    alert('好友添加成功！');
  } catch (error) {
    alert('添加好友失败：' + (error as Error).message);
  }
};

const handleViewProfile = (player: Player) => {
  router.push(`/player/${player.id}`);
};

onMounted(async () => {
  // 确保玩家数据已加载
  if (playerStore.players.length === 0) {
    try {
      await playerStore.loadPlayers();
    } catch (error) {
      console.error('加载玩家数据失败:', error);
    }
  }
});
</script>

<style scoped>
.players {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
  padding: 24px 0;
  border-bottom: 1px solid var(--color-border);
}

.page-header h1 {
  font-size: 32px;
  font-weight: 700;
  color: var(--color-text);
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 16px;
  align-items: center;
}

.search-box {
  position: relative;
}

.search-input {
  padding: 10px 16px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 14px;
  width: 250px;
  transition: border-color 0.2s ease;
}

.search-input:focus {
  outline: none;
  border-color: var(--color-primary);
}

.sort-controls {
  display: flex;
  align-items: center;
}

.sort-select {
  padding: 10px 16px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 14px;
  background: white;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

.sort-select:focus {
  outline: none;
  border-color: var(--color-primary);
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .header-actions {
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }
  
  .search-input {
    width: 100%;
  }
}
</style>